import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import Index from '../views/users/Index.vue'
import Rights from '../views/rights/Rights.vue'
import Roles from '../views/rights/Roles.vue'
import Cates from '../views/goods/Cates.vue'
import Params from '../views/goods/Params.vue'
import Goodslist from '../views/goods/Goodslist.vue'
import AddGoods from '../views/goods/AddGoods.vue'
import Orders from '../views/orders/Orders.vue'
import Reports from '../views/reports/Reports.vue'
// import { component } from 'vue/types/umd'

Vue.use(VueRouter)

const routes = [
  //  重定向到  /login
  {
    path: '/',
    redirect: '/login'
  },
  //  路由规则
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      // 欢迎页面
      {
        path: '/welcome',
        component: Welcome
      },
      // 用户页面
      {
        path: '/users',
        component: Index
      },
      // 权限页面
      {
        path: '/rights',
        component: Rights
      },
      // 角色页面
      {
        path: '/roles',
        component: Roles
      },
      // 商品列表
      {
        path: '/categories',
        component: Cates
      },
      // 参数页面
      {
        path: '/params',
        component: Params
      },
      // 商品列表页
      {
        path: '/goods',
        component: Goodslist
      },
      // 商品添加页面
      {
        path: '/add/goods/',
        component: AddGoods
      },
      // 订单页面
      {
        path: '/orders',
        component: Orders
      },
      // 数据统计
      {
        path: '/reports',
        component: Reports
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem('token')
  // 判断是否登录
  if (token) {
    next()
  } else {
    // 没登录还要判断是否访问login页面
    if (to.path === '/login') return next()
    next('/login')
  }
  // if (!token) return next('/login')
  // next()
})

export default router
